package com.bjqh.edu.UI.Activity.Index;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;

import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.StudentPersonal.PaymentActivity;

import java.nio.charset.Charset;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FreeReservation2Activity extends AppCompatActivity {
    //是撒多
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.webview)
    WebView webview;
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_free_reservation);
        ButterKnife.bind(this);
        url = getIntent().getStringExtra("url");
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //  tvTitle.setText("免费预约");
        WebSettings webSettings = webview.getSettings();
        // 设置与Js交互的权限
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        // 设置允许JS弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        webview.loadUrl(url);
        webview.setWebViewClient(new WebViewClient() {


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("网址信息", url);
                if (url.startsWith("qianhexuetang://")) {
                    if (Uri.parse(url).getQueryParameter("qianhexuetang")!=null&&Uri.parse(url).getQueryParameter("qianhexuetang").equals("qianhexuetangpay"))
                    {
                        Intent intent = new Intent();
                        intent.putExtra("price", Uri.parse(url).getQueryParameter("price"));
                        intent.putExtra("coursedes", Uri.parse(url).getQueryParameter("goodtitle"));
                        intent.putExtra("coursename", Uri.parse(url).getQueryParameter("goodname"));
                        intent.putExtra("cid", Uri.parse(url).getQueryParameter("goodid"));
                        intent.setClass(FreeReservation2Activity.this, PaymentActivity.class);
                        startActivity(intent);
                    }

                }
                else {
                    view.loadUrl(url);

                }
                return false;
            }
        });
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                tvTitle.setText(title);
            }
        /*    @Override
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                AlertDialog.Builder b = new AlertDialog.Builder(FreeReservationActivity.this);
                b.setTitle("Confirm");
                b.setMessage(message);
                b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm();
                    }
                });
                b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.cancel();
                    }
                });
                b.create().show();
                return true;
            }*/
        });
    }

    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /*  //销毁Webview
      @Override
      protected void onDestroy() {
          if (webview != null) {
              webview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
              webview.clearHistory();
              ((ViewGroup) webview.getParent()).removeView(webview);
              webview.destroy();
              webview = null;
          }
          super.onDestroy();
      }*/
    public void onResume() {
        super.onResume();
        if (webview != null) {
            webview.onResume();
            webview.resumeTimers();
        }
    }

    @Override
    public void onPause() {
        if (webview != null) {
            webview.onPause();
            webview.pauseTimers();
        }
        super.onPause();
    }
}
